﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
namespace LinkCard.AdminCP.Deal
{
    public partial class deal_edit : System.Web.UI.Page
    {
        protected bool nInTemp;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!IsPostBack)
            {
                Int64 nDealID = ConvertUtility.ToInt64(Request.QueryString["DealID"]);
                DealID.Value = nDealID.ToString();
                Int64 nCategoryID = 0;
                Int64 nSupplierID = 0;

                if (nDealID > 0)
                {
                    btnUpdate.Visible = false;
                    btnPublish.Visible = false;
                    btnDel.Visible = false;
                    btnAccept.Visible = false;
                    btnDrop.Visible = false;

                    string sSQL = "";
                    nInTemp = inTemp(nDealID);
                    if (nInTemp)
                    {
                        sSQL = "SELECT * FROM DealTemp WHERE DealID =" + nDealID.ToString() + "";
                        btnAccept.Visible = true;
                        btnDrop.Visible = true;

                    }
                    else
                    {
                        sSQL = "SELECT * FROM Deal WHERE DealID =" + nDealID.ToString() + "";
                        btnPublish.Visible = true;
                        btnDel.Visible = true;

                    }

                    DataTable dtDeal = DataHelper.SelectTable(sSQL, false);
                    if (dtDeal.Rows.Count > 0)
                    {


                        nCategoryID = ConvertUtility.ToInt64(dtDeal.Rows[0]["CategoryID"]);
                        nSupplierID = ConvertUtility.ToInt64(dtDeal.Rows[0]["SupplierID"]);
                        Name.Value = dtDeal.Rows[0]["Name"].ToString();
                        Description.Value = dtDeal.Rows[0]["Description"].ToString();
                        Detail.Value = dtDeal.Rows[0]["Detail"].ToString();
                        DealImage.Value = dtDeal.Rows[0]["Image"].ToString();
                        Price.Value = dtDeal.Rows[0]["Price"].ToString();
                        Discounting.Value = dtDeal.Rows[0]["Discounting"].ToString();
                        TopLinkRate.Value = dtDeal.Rows[0]["TopLinkRate"].ToString();
                        Point.Value = dtDeal.Rows[0]["Point"].ToString();
                        EndDate.Value = dtDeal.Rows[0]["EndDate"].ToString();
                        PostDate.Value = dtDeal.Rows[0]["PostDate"].ToString();
                        //AlbumID.Value = dtDeal.Rows[0]["AlbumID"].ToString();
                        Status.Value = dtDeal.Rows[0]["Status"].ToString();
                        txtTags.Value = dtDeal.Rows[0]["Tags"].ToString();
                        if (ConvertUtility.ToByte(dtDeal.Rows[0]["Status"].ToString()) == 0) btnUpdate.Visible = true;

                    }

                    DataTable dt;
                    // Supplier Option

                    dt = DataHelper.SelectTable("SELECT SupplierID, Name FROM Supplier where Status = 2 ORDER BY Name", false);
                    SupplierID.DataSource = dt;
                    SupplierID.DataTextField = "Name";
                    SupplierID.DataValueField = "SupplierID";
                    SupplierID.DataBind();
                    SupplierID.SelectedValue = nSupplierID.ToString();


                    // Category Option 

                    dt = DataHelper.SelectTable("SELECT CategoryID, [Text] FROM Category where [level] = 2  and isDisplay = 1 ORDER BY [Text]", false);
                    CategoryID.DataSource = dt;
                    CategoryID.DataTextField = "Text";
                    CategoryID.DataValueField = "CategoryID";
                    CategoryID.DataBind();
                    CategoryID.SelectedValue = nCategoryID.ToString();

                }
            }

        }

        protected bool inTemp(Int64 nDealID)
        {

            DataTable dtDealTemp = DataHelper.SelectTable("SELECT DealID FROM DealTemp WHERE DealID =" + nDealID.ToString() + "", false);
            if (dtDealTemp.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }

        protected void btnPublish_Click(object sender, EventArgs e)
        {
            addDealTemp(getUserID());
            if (ConvertUtility.ToByte(Status.Value) == 2)
            {
                Response.Redirect("/Deal/deal_all_published.aspx");
            }
            else
            {
                updateDeal(1);
                Response.Redirect("/Deal/deal_list.aspx");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            updateDeal(0);

            Response.Redirect("/Deal/deal_list.aspx");

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string sSQL = "delete Deal where DealID = " + DealID.Value; ;
            DataHelper.ExecuteNonQuery(sSQL, true);
            if (ConvertUtility.ToByte(Status.Value) == 2)
            {
                Response.Redirect("/Deal/deal_all_published.aspx");
            }
            else
            {
                Response.Redirect("/Deal/deal_list.aspx");
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            updateDeal(2);
            // Xóa Temp
            string sSQL = "delete DealTemp where DealID = " + DealID.Value; ;
            DataHelper.ExecuteNonQuery(sSQL, true);
            Response.Redirect("/Deal/deal_all_pending.aspx");
        }

        protected void btnDrop_Click(object sender, EventArgs e)
        {
            // Xóa Deal

            string sSQL = "delete Deal where [Status] = 1 and DealID = " + DealID.Value; ;
            DataHelper.ExecuteNonQuery(sSQL, true);
            Response.Redirect("/Deal/deal_all_pending.aspx");

            // Xóa temp
            string sSQLTemp = "delete DealTemp where DealID = " + DealID.Value; ;
            DataHelper.ExecuteNonQuery(sSQLTemp, true);
            Response.Redirect("/Deal/deal_all_pending.aspx");
        }

        protected void updateDeal(byte status)
        {


            var dao = new DealDao();
            var _deal = new LinkCard.Model.Deal();

            _deal.CategoryID = ConvertUtility.ToInt64(CategoryID.SelectedValue);
            _deal.SupplierID = ConvertUtility.ToInt64(SupplierID.SelectedValue);
            _deal.Name = Name.Value;
            _deal.Description = Description.Value;
            _deal.Detail = Detail.Value;
            _deal.Image = DealImage.Value;
            _deal.Price = ConvertUtility.ToDecimal(Price.Value);
            _deal.Discounting = ConvertUtility.ToDouble(Discounting.Value);
            _deal.TopLinkRate = ConvertUtility.ToDouble(TopLinkRate.Value);
            _deal.Point = ConvertUtility.ToInt32(Point.Value);
            _deal.EndDate = ConvertUtility.ToDateTime(EndDate.Value);
            _deal.PostDate = ConvertUtility.ToDateTime(PostDate.Value);
            _deal.Tags = txtTags.Value;
            _deal.Status = status;
            _deal.DealID = ConvertUtility.ToInt64(DealID.Value);
            dao.Update(_deal);

            if (!string.IsNullOrEmpty(_deal.Tags))
            {
                var addTag = new TagDao().AddDealTag(_deal.DealID, _deal.Tags);
            }
        }

        protected void addDealTemp(Int64 nUserID)
        {

            var dao = new DealTempDao();
            var _deal = new LinkCard.Model.DealTemp();

            _deal.CategoryID = ConvertUtility.ToInt64(CategoryID.SelectedValue);
            _deal.SupplierID = ConvertUtility.ToInt64(SupplierID.SelectedValue);
            _deal.Name = Name.Value;
            _deal.Description = Description.Value;
            _deal.Detail = Detail.Value;
            _deal.Image = DealImage.Value;
            _deal.Price = ConvertUtility.ToDecimal(Price.Value);
            _deal.Discounting = ConvertUtility.ToDouble(Discounting.Value);
            _deal.TopLinkRate = ConvertUtility.ToDouble(TopLinkRate.Value);
            _deal.Point = ConvertUtility.ToInt32(Point.Value);
            _deal.EndDate = ConvertUtility.ToDateTime(EndDate.Value);
            _deal.PostDate = ConvertUtility.ToDateTime(PostDate.Value);
            _deal.Status = 1;
            _deal.DealID = ConvertUtility.ToInt64(DealID.Value);
            _deal.DateLog = DateTime.Now;
            _deal.UserID = nUserID;
            _deal.Tags = txtTags.Value;
            dao.Add(_deal);


        }
    }
}